package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.guigu.buy.pojo.Customer;
import com.guigu.buy.pojo.Order;
import com.guigu.buy.service.ICustomerService;
import com.guigu.buy.utils.PageUtils;
import com.guigu.buy.vo.CustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/customer")
@ResponseBody
public class CustomerController {
    @Autowired
    private ICustomerService customerService;


    @ResponseBody
    @GetMapping("/selectLogin")
    public Customer selectLogin(String userAccount, String userPassword, HttpSession session){
        System.out.println(userAccount+userPassword);
        Customer login = customerService.selectLogin(userAccount,userPassword);
        System.out.println(login);
        session.setAttribute("login",login);

        return login;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/getById/{id}")
    public Customer getById(@PathVariable Integer id){

        Customer byId = customerService.getById(id);
        System.out.println(byId);
        return byId;
    }

    /**
     *  根据id修改前台用户信息
     * @param customer
     * @return
     */
    @ResponseBody
    @PostMapping("/updateUserInfo")
    public int updateUserInfo(@RequestBody Customer customer){
        System.out.println(customer);
        int i = customerService.updateUserInfo(customer);
        System.out.println(i);
        return i;
    }


    /**
     * 查询前台用户表
     * @param customer
     * @return
     */
    @ResponseBody
    @PostMapping("/selectCustomer")
    public List<Order> selectCustomer(@RequestBody Customer customer){
        List<Order> customers = customerService.selectCustomer(customer);
        return customers;

    }

    /**
     * 头像上传
     * @return
     */
    @RequestMapping("/avatarImgUpload")
    public String avatarImgUpload(@RequestBody MultipartFile file) {
        System.out.println(file);
        //获取源文件名(包含后缀 1.jpg)
        String originalFilename = file.getOriginalFilename();
        //截取后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成唯一文件名
        String newFileName = UUID.randomUUID().toString()+suffix;
        //该文件要放在指定目录下
        File dir = new File("D:/img");
        if(!dir.exists())
            dir.mkdir();
        //目标文件对象
        File destFile = new File(dir,newFileName);

        //文件写入
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:10002/"+newFileName;
    }

    @Autowired
    private ICustomerService iCustomerService;


    //登录
    @RequestMapping("/getCustomer")
    @ResponseBody
    public Customer getCustomer(@RequestBody  Customer customer){
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("user_account",customer.getUserAccount());
        wrapper.eq("user_password",customer.getUserPassword());
        Customer one=iCustomerService.getOne(wrapper);
        return one;
    }
    //注册
    @RequestMapping("/sign")
    @ResponseBody
    public boolean getSign(@RequestBody  Customer customer){
        System.out.println("数据数据！！1"+customer);
        Customer customer1=new Customer();
        customer1.setId(null);
        customer1.setUserName(customer.getUserName());
        customer1.setUserAccount(customer.getUserAccount());
        customer1.setUserPassword(customer.getUserPassword());
        customer1.setUserPhone(customer.getUserPhone());
        customer1.setUserMechant(0);
        customer1.setUserSupplier(0);
        customer1.setUserStatus(0);
        customer1.setCreateTime(new Date());
        customer1.setUpdateTime(new Date());
        System.out.println("数据数据！！1"+customer1);
        boolean sign=iCustomerService.save(customer1);
        return sign;
    }
    //忘记密码 查询手机号跟账号是否有误
    @RequestMapping("/forgotPassword")
    @ResponseBody
    public Customer getForgotPassword(@RequestBody  Customer customer){
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("user_account",customer.getUserAccount());
        wrapper.eq("user_phone",customer.getUserPhone());
        wrapper.eq("user_password",customer.getUserPassword());
        Customer one=iCustomerService.getOne(wrapper);
        return one;
    }
    //忘记密码 查询手机号跟账号无误  修改密码
    @RequestMapping("/upForgotPassword/{id}")
    @ResponseBody
    public boolean getupForgotPassword(@RequestBody  Customer customer,@PathVariable int id){
        Customer customer1=new Customer();
        customer1.setUserPassword(customer.getUserPassword());
        System.out.println("1"+id);
        customer1.setId(id);
        boolean forgotPassword=iCustomerService.updateById(customer1);
        return forgotPassword;
    }
    @RequestMapping("/dsahgdasuguy/{uid}")
    @ResponseBody
    public Customer CustomerId(@PathVariable int uid ) {
        return iCustomerService.getById(uid);
    }

    //修改
    @RequestMapping("/xg")
    @ResponseBody

    public String xg(@RequestBody Customer customer) {
        boolean flag = this.iCustomerService.updateById(customer);
        if (flag) {

            return "ok";
        }
        return "error";
    }

    /**
     * 分页查询用户
     * @param pageNo
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping("/list/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Customer> list(@PathVariable int pageNo, @PathVariable int pageSize, @RequestBody CustomerVo vo) {
        PageUtils<Customer> alllist = customerService.Alllist(pageNo, pageSize, vo);
        return alllist;
    }

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public Customer getOne(int id) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Customer one = customerService.getOne(wrapper);
        return one;
    }
}
